/*
 * @message: 描述
 * @Author: Roy
 * @Email: cx_love_xc@163.com
 * @Github: cx_love_xc@163.com
 * @Date: 2022-01-11 14:46:26
 * @LastEditors: Roy
 * @LastEditTime: 2022-01-18 11:44:39
 * @Deprecated: 否
 * @FilePath: /blog/src/front/container/home/index.jsx
 */

import React, { useState, useEffect } from 'react';
import { Helmet } from 'react-helmet'
import request from '../../../common/request'
import { parseJsonByString } from '../../../common/util'
import Banner from '../component/banner'
import Footer from '../component/footer'
import List from '../component/list'

// const pageSchema = parseJsonByString(localStorage.schema, {});
// const { children = [], attributes = {} } = pageSchema;

const map = { Banner, Footer, List };

const render = (item, index) => {
    const Component = map[item.name];
    return Component ? <Component key={index} schema={item} /> : null;

}


const Home = () => {
    const [pageSchema, setPageSchema] = useState({});
    const { children = [], attributes = {} } = pageSchema;
    useEffect(() => {
        request.get('/api/schema/getLastOne').then((res) => {
            const data = res?.data;
            data && setPageSchema(parseJsonByString(data.schema, {}));
        });
    }, [])

    return (
        <div>
            <Helmet>
                <title>{attributes?.title || ''}</title>
            </Helmet>
            {
                children.map((item, index) => {
                    return render(item, index)
                })
            }
        </div>
    );
}

export default Home;